package com.phkj.qdh.ui.dialog

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hjq.shape.view.ShapeButton
import com.hjq.toast.ToastUtils
import com.lxj.xpopup.core.BottomPopupView
import com.phkj.qdh.R
import com.phkj.qdh.bean.ApprovalManBean
import com.phkj.qdh.ui.adapter.SelectApprovalManAdapter
import com.phkj.qdh.utils.MyItemDecoration

class SelectManDialog(context:Context,val data:ArrayList<ApprovalManBean>):BottomPopupView(context) {
    private val selectApprovalManAdapter by lazy { SelectApprovalManAdapter() }
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_select_man
    }


    override fun onCreate() {
        super.onCreate()
        val rv = findViewById<RecyclerView>(R.id.rv_item)
        val btn = findViewById<ShapeButton>(R.id.btn_confirm)
        rv.run {
            layoutManager = LinearLayoutManager(context)
            adapter = selectApprovalManAdapter
            addItemDecoration(MyItemDecoration(width = 10))
        }

        selectApprovalManAdapter.addAll(data)
        selectApprovalManAdapter.setOnItemClickListener{adapter,_,pos->
            adapter.getItem(pos)!!.isSelected = !adapter.getItem(pos)!!.isSelected
            adapter.notifyItemChanged(pos)
        }

        btn.setOnClickListener {
            val data = selectApprovalManAdapter.items.filter { it.isSelected }
            if (data.isNotEmpty()){
                selectManListener?.ok(data)
                dismiss()
            }else{
                ToastUtils.show("请选择审批人")
            }
        }

    }

    private var selectManListener:SelectManListener? = null
    fun setSelectManListener(listener:SelectManListener){
        this.selectManListener = listener
    }

}


interface SelectManListener{
    fun ok(list:List<ApprovalManBean>)
}